import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter路由示例',
      theme: ThemeData(primarySwatch: Colors.blue),
      initialRoute: '/',
      onGenerateRoute: (RouteSettings settings) {
        final args = settings.arguments;
        switch (settings.name) {
          case '/':
            return MaterialPageRoute(builder: (_) => const HomePage());
          case '/detail':
            if (args is String) {
              return MaterialPageRoute(builder: (_) => DetailPage(title: args));
            }
            return _errorRoute();
          case '/login':
            return MaterialPageRoute(builder: (_) => const LoginPage());
          case '/profile':
            return MaterialPageRoute(builder: (_) => const ProfilePage());
          default:
            return _errorRoute();
        }
      },
    );
  }

  static Route<dynamic> _errorRoute() {
    return MaterialPageRoute(
      builder: (_) {
        return Scaffold(
          appBar: AppBar(title: const Text('错误页面')),
          body: const Center(child: Text('页面未找到!')),
        );
      },
    );
  }
}

class HomePage extends StatelessWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('首页')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              child: const Text('使用命名路由跳转详情页'),
              onPressed: () {
                Navigator.pushNamed(context, '/detail', arguments: '命名路由参数');
              },
            ),
            const SizedBox(height: 20),
            ElevatedButton(
              child: const Text('使用普通路由跳转详情页'),
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => const DetailPage(title: '普通路由参数'),
                  ),
                );
              },
            ),
            const SizedBox(height: 20),
            ElevatedButton(
              child: const Text('嵌套路由示例'),
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => const NestedRoutePage(),
                  ),
                );
              },
            ),
            const SizedBox(height: 20),
            ElevatedButton(
              child: const Text('登录示例'),
              onPressed: () {
                Navigator.pushNamed(context, '/login');
              },
            ),
          ],
        ),
      ),
    );
  }
}

class DetailPage extends StatelessWidget {
  final String title;

  const DetailPage({Key? key, required this.title}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('详情页 - $title')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text('传递的参数: $title'),
            const SizedBox(height: 20),
            ElevatedButton(
              child: const Text('返回首页并携带结果'),
              onPressed: () {
                Navigator.pop(context, '这是来自详情页的返回结果');
              },
            ),
          ],
        ),
      ),
    );
  }
}

class NestedRoutePage extends StatelessWidget {
  const NestedRoutePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('嵌套路由示例')),
      body: Navigator(
        initialRoute: 'nested/home',
        onGenerateRoute: (RouteSettings settings) {
          switch (settings.name) {
            case 'nested/home':
              return MaterialPageRoute(
                builder: (context) => const NestedHomePage(),
              );
            case 'nested/profile':
              return MaterialPageRoute(
                builder: (context) => const NestedProfilePage(),
              );
            default:
              return MaterialPageRoute(
                builder:
                    (context) =>
                        const Scaffold(body: Center(child: Text('嵌套路由错误页'))),
              );
          }
        },
      ),
    );
  }
}

class NestedHomePage extends StatelessWidget {
  const NestedHomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('嵌套首页')),
      body: Center(
        child: ElevatedButton(
          child: const Text('跳转到嵌套个人页'),
          onPressed: () {
            Navigator.pushNamed(context, 'nested/profile');
          },
        ),
      ),
    );
  }
}

class NestedProfilePage extends StatelessWidget {
  const NestedProfilePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('嵌套个人页')),
      body: Center(
        child: ElevatedButton(
          child: const Text('返回嵌套首页'),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
      ),
    );
  }
}

class LoginPage extends StatefulWidget {
  const LoginPage({Key? key}) : super(key: key);

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  bool _isLoggedIn = false;

  void _login() {
    // 模拟登录逻辑
    setState(() {
      _isLoggedIn = true;
    });
    // 登录成功后跳转到个人页
    Navigator.pushReplacementNamed(context, '/profile');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('登录页')),
      body: Center(
        child: ElevatedButton(child: const Text('登录'), onPressed: _login),
      ),
    );
  }
}

class ProfilePage extends StatelessWidget {
  const ProfilePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('个人页')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Text('这是个人页内容'),
            const SizedBox(height: 20),
            ElevatedButton(
              child: const Text('返回首页'),
              onPressed: () {
                Navigator.popUntil(context, ModalRoute.withName('/'));
              },
            ),
          ],
        ),
      ),
    );
  }
}
